<!DOCTYPE html>
<html>
  <!-- Html Head Tag-->
  <head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="Rms">
  <!-- Open Graph Data -->
  <meta property="og:title" content="Linux安装MySQL"/>
  <meta property="og:description" content="" />
  <meta property="og:site_name" content="帅的一逼"/>
  <meta property="og:type" content="article" />
  <meta property="og:image" content="http://shsa.gitee.io"/>
  
    <link rel="alternate" href="/atom.xml" title="帅的一逼" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  

  <!-- Site Title -->
  <title>帅的一逼</title>

  <!-- Bootstrap CSS -->
  
<link rel="stylesheet" href="/myblog/css/bootstrap.min.css">

  <!-- Custom CSS -->
  
  
<link rel="stylesheet" href="/myblog/css/style.light.css">


  <!-- Google Analytics -->
  

<meta name="generator" content="Hexo 4.2.1"></head>

  <body>
    <!-- Page Header -->


<header class="site-header header-background" style="background-image: url(/myblog//img/i.jpg)">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <div class="page-title with-background-image">
          <p class="title">Linux安装MySQL</p>
          <p class="subtitle"></p>
        </div>
        <div class="site-menu with-background-image">
          <ul>
            
              <li>
                <a href="/myblog/">
                  
                  Home
                  
                </a>
              </li>
            
              <li>
                <a href="/myblog/archives">
                  
                  Archives
                  
                </a>
              </li>
            
              <li>
                <a href="/myblog/about">
                  
                  About
                  
                </a>
              </li>
            
          </ul>
        </div>
      </div>
    </div>
  </div>
</header>

<article>
  <div class="container typo">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <div class="post-info text-muted">
          
            <!-- Author -->
            <span class="author info">By Rms</span>
          
          <!-- Date -->
          <span class="date-time info">On
            <span class="date">2020-07-16</span>
            <span class="time">21:53:53</span>
          </span>
          
        </div>
        <!-- Tags -->
        
        <!-- Post Main Content -->
        <div class="post-content">
          <h1 id="Linux安装MySQL"><a href="#Linux安装MySQL" class="headerlink" title="Linux安装MySQL"></a>Linux安装MySQL</h1><p><strong>一、下载mysql yum包</strong></p>
<p>官方下载地址:<a href="https://dev.mysql.com/downloads/repo/yum/" target="_blank" rel="noopener">https://dev.mysql.com/downloads/repo/yum/</a><br>或者直接使用wget下载(注意:下载mysql需要有oracle账号,oracle公司产品都需要oracle账号才能下载)</p>
<p>下载自己需要的mysql版本就行，下载连接可以通过浏览器获取</p>
<img src="https://pic.baixiongz.com/2020/07/16/1ddce40b8cefd.png" alt="image.png" style="zoom:50%;" />

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">使用wget进行下载</span></span><br><span class="line">wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm</span><br></pre></td></tr></table></figure>

<p>二、安装软件源</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm</span><br></pre></td></tr></table></figure>

<p>三、安装mysql</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yum install mysql-community-server</span><br><span class="line">//安装过程中如果出现y/n的话就一直y就行了，如果嫌麻烦可以用下面这个命令来安装</span><br><span class="line">yum install -y mysql-community-server</span><br></pre></td></tr></table></figure>

<p><strong>注意:</strong></p>
<p>如果yum命令报Another app is currently holding the yum lock; waiting for it to exit…</p>
<p>可以通过强制关掉yum进程：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rm -f /var/run/yum.pid</span><br></pre></td></tr></table></figure>

<p>四、启动&amp;配置</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//启动</span><br><span class="line">service mysqld start</span><br><span class="line">//查看运行状态</span><br><span class="line">service mysqld status</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">看到绿色的active(running)表示启动成功</span></span><br><span class="line">    active(running)</span><br></pre></td></tr></table></figure>

<p>然后mysql在5.6之后的版本都会默认生成一个默认密码，是root用户的。通过如下命令查看密码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep 'temporary password' /var/log/mysqld.log</span><br></pre></td></tr></table></figure>

<p>用红色框标识出来的便是默认设置的密码</p>
<img src="https://pic.baixiongz.com/2020/07/16/3c1a70574d60b.png" style="zoom:50%;" />

<p>五、进入mysql</p>
<p>执行完如下命令之后输入默认密码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -u root -p</span><br></pre></td></tr></table></figure>

<p>六、创建用户和分配权限</p>
<p>由于mysql8新提供了一种密码加密方式caching-sha2-password，且为默认，目前很多客户端都不支持，所以我们在创建新角色用户的时候可以指定其为mysql_native_password，原来的root账户等不去做任何改变</p>
<p><strong>(新的密码规则要求大小写字母数字特殊字符都要有)</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//创建一个test用户(一般情况下root用户只在本地环境下使用),密码是29dIg;2^，数据库的密码最好设置得连自己都记不住。</span><br><span class="line">CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '29dIg;2^';</span><br><span class="line">//授权数据库给用户，并设置所有ip都可以远程连接</span><br><span class="line">//如果只授予用户某些数据库的话就把*.*改成 数据库名称.*  意思就是 数据库.表</span><br><span class="line">//如果只授予用户某些权限的话，比如只能读不能写，就把GRANT ALL改成GRANT SELECT</span><br><span class="line">GRANT ALL ON *.* TO 'test'@'%';</span><br><span class="line">//当然，你也可以撤销授权,用法和授权几乎都是一样的。</span><br><span class="line">REVOKE ALL ON *.* from 'test'@'%';</span><br></pre></td></tr></table></figure>

<p>最后使设置立即生效</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flush privileges;</span><br></pre></td></tr></table></figure>

<p>七、修改密码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1ALTER USER `root`@`localhost` IDENTIFIED BY '新密码';</span><br></pre></td></tr></table></figure>

<p>退出mysql</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">EXIT</span><br><span class="line">//或者</span><br><span class="line">QUIT</span><br></pre></td></tr></table></figure>

<p>注意:</p>
<p>安装完后别忘了开放对应端口,一般是3306,阿里云可以通过安全组开放,本地虚拟机则通过防火墙命令开放</p>
<p>CentOS7.0以上默认使用firewall防火墙</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 查询端口是否开放</span><br><span class="line">firewall-cmd --query-port=3306/tcp</span><br><span class="line">// 开放3306端口</span><br><span class="line">firewall-cmd --permanent --add-port=3306/tcp</span><br><span class="line">// 重启防火墙(修改配置后要重启防火墙)</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>

<p>CentOS7.0以下使用iptables防火墙</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 开启3306端口</span><br><span class="line">vim /etc/sysconfig/iptables</span><br><span class="line">// 加入如下代码(按i进入编辑模式)</span><br><span class="line">-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT</span><br><span class="line">// 保存退出后重启防火墙 (按ESC输入:wq按ENTER保存退出)</span><br><span class="line">service iptables restart</span><br></pre></td></tr></table></figure>



<p>原文转载:<a href="https://all_jun.gitee.io/blog/2020/07/14/CentOS安装mysql/" target="_blank" rel="noopener">https://all_jun.gitee.io/blog/2020/07/14/CentOS%E5%AE%89%E8%A3%85mysql/</a></p>

        </div>
      </div>
    </div>
  </div>
</article>



    <!-- Footer -->
<footer>
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <p class="copyright text-muted">
          Theme By <a target="_blank" href="https://github.com/levblanc">Levblanc.</a>
          Inspired By <a target="_blank" href="https://github.com/klugjo/hexo-theme-clean-blog">Clean Blog.</a>
        <p class="copyright text-muted">
          Powered By <a target="_blank" href="https://hexo.io/">Hexo.</a>
        </p>
      </div>
    </div>
  </div>
</footer>


    <!-- After Footer Scripts -->

<script src="/myblog/js/highlight.pack.js"></script>

<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    var codeBlocks = Array.prototype.slice.call(document.getElementsByTagName('pre'))
    codeBlocks.forEach(function(block, index) {
      hljs.highlightBlock(block);
    });
  });
</script>

  </body>
</html>

